#include "CvCommand.h"

#include <QProcess>
#include <QThread>

QString CvCommand::_workingDir = "";

CvCommand::CvCommand(QString command, QObject *parent) :
    QObject(parent),
    _status( CommandResult::BeforeStart ),
    _command( command ) {
}

CommandResult CvCommand::getStatus() {
    return _status;
}

void CvCommand::setWorkingDir(QString dir) {
    _workingDir = dir;
}

void CvCommand::process() {
    Q_ASSERT(!_command.isEmpty());
    _status = CommandResult::Active;
    QProcess svnStatus;
    svnStatus.setWorkingDirectory(_workingDir);
    svnStatus.start(_command);
    svnStatus.waitForFinished();
    //TODO: куда-то пропали ошибки
    QString error = svnStatus.readAllStandardError();
    if (error.length()) {
        _status = CommandResult::Error;
        _result = error;
    } else {
        _result = svnStatus.readAllStandardOutput();
        _status = CommandResult::Finished;
    }
    emit finished();
    emit killThread();
}

void CvCommand::finish() {
    emit killThread();
    deleteLater();
}

QString CvCommand::getResult() {
    return _result;
}
